package proxydemo;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class Demo {

    interface IProxy{
        void eat();
    }

    static class Human implements IProxy{

        @Override
        public void eat() {
            System.out.println("eat !!!! ");
        }
    }

    public static void main(String[] args) {
        Human human = new Human();
        IProxy o = (IProxy)Proxy.newProxyInstance(IProxy.class.getClassLoader(), new Class[]{IProxy.class},
                (proxy, method, args1) ->
                {
                    System.out.println("代理开始---");
                    // 被代理对象的方法可执行可不执行
                    method.invoke(human);
                    System.out.println("代理结束---");
                    return proxy;
                });
        o.eat();
    }

}

